//
//  UIImage+SLBuddle.m
//  SLAppProduct
//
//  Created by AlanWang on 2018/3/7.
//  Copyright © 2018年 Sunline. All rights reserved.
//

#import "UIImage+AWBuddle.h"
#import <UIKit/UIKit.h>
#import "JFBase.h"

@implementation UIImage (AWBuddle)
+(UIImage*)imageNameFromBuddle:(NSString*)name type:(NSString*)type buddleName:(NSString*)buddlename directoryInBuddle:(NSString*)directory{
    
    UIImage * image = nil;
    NSString * themeImageName = name;
    NSString *buddlePath = [[NSBundle mainBundle] pathForResource:buddlename ofType:@"bundle"];
    NSString *imagePath = [[NSBundle bundleWithPath:buddlePath] pathForResource:themeImageName ofType:type];
    NSString *imagePath2x = [[NSBundle bundleWithPath:buddlePath] pathForResource:[NSString stringWithFormat:@"%@@2x",themeImageName] ofType:type];
    NSString *imagePath3x = [[NSBundle bundleWithPath:buddlePath] pathForResource:[NSString stringWithFormat:@"%@@3x",themeImageName] ofType:type];
    if([name containsString:@"@"]){
        imagePath = [[NSBundle bundleWithPath:buddlePath] pathForResource:themeImageName ofType:type];
        imagePath2x = [[NSBundle bundleWithPath:buddlePath] pathForResource:[NSString stringWithFormat:@"%@",themeImageName] ofType:type];
        imagePath3x = [[NSBundle bundleWithPath:buddlePath] pathForResource:[NSString stringWithFormat:@"%@",themeImageName] ofType:type];
    }


    
    if(3 == [UIScreen mainScreen].scale) //3x下优先获取高清图片
    {
        image = [UIImage imageWithContentsOfFile:imagePath3x];
        if(nil == image)
        {
            image = [UIImage imageWithContentsOfFile:imagePath2x];
        }
    }
    else if(2 == [UIScreen mainScreen].scale)
    {
        image = [UIImage imageWithContentsOfFile:imagePath2x];
        if(nil == image)
        {
            image = [UIImage imageWithContentsOfFile:imagePath];
        }
    }
    if(nil == image)
    {
        image = [UIImage imageWithContentsOfFile:imagePath];
        if(nil == image)
        {
          //  NSString * tips =[NSString stringWithFormat:@"找不到对应的图片-%@",name];
          //  NSLog(@"%@",tips);
        }
    }
    return image;
}
+(UIImage*)imageNameFromBuddle:(NSString*)name type:(NSString*)type buddleName:(NSString*)buddlename{
    
    UIImage * image = nil;
    NSString * themeImageName = name;
    NSString *buddlePath = [[NSBundle mainBundle] pathForResource:buddlename ofType:@"bundle"];
    NSString *imagePath = [[NSBundle bundleWithPath:buddlePath] pathForResource:themeImageName ofType:type];
    NSString *imagePath2x = [[NSBundle bundleWithPath:buddlePath] pathForResource:[NSString stringWithFormat:@"%@@2x",themeImageName] ofType:type];
    NSString *imagePath3x = [[NSBundle bundleWithPath:buddlePath] pathForResource:[NSString stringWithFormat:@"%@@3x",themeImageName] ofType:type];
    if([name containsString:@"@"]){
        imagePath = [[NSBundle bundleWithPath:buddlePath] pathForResource:themeImageName ofType:type];
        imagePath2x = [[NSBundle bundleWithPath:buddlePath] pathForResource:[NSString stringWithFormat:@"%@",themeImageName] ofType:type];
       imagePath3x = [[NSBundle bundleWithPath:buddlePath] pathForResource:[NSString stringWithFormat:@"%@",themeImageName] ofType:type];
    }

    
    if(3 == [UIScreen mainScreen].scale) //3x下优先获取高清图片
    {
        image = [UIImage imageWithContentsOfFile:imagePath3x];
        if(nil == image)
        {
            image = [UIImage imageWithContentsOfFile:imagePath2x];
        }
    }
    else if(2 == [UIScreen mainScreen].scale)
    {
        image = [UIImage imageWithContentsOfFile:imagePath2x];
        if(nil == image)
        {
            image = [UIImage imageWithContentsOfFile:imagePath];
        }
    }
    if(nil == image)
    {
        image = [UIImage imageWithContentsOfFile:imagePath];
        if(nil == image)
        {
//            NSString * tips =[NSString stringWithFormat:@"找不到对应的图片-%@-%@",name,buddlename];
//            NSLog(@"%@",tips);
        }
    }
    if(!image){//按屏幕分辨率获取图未获取到就检测是否有图片 有的话 优先选择高倍图
        if ([JFStringHelper isNotEmpty:imagePath3x]) {
            image = [UIImage imageWithContentsOfFile:imagePath3x];
        }else if ([JFStringHelper isNotEmpty:imagePath2x]){
             image = [UIImage imageWithContentsOfFile:imagePath2x];
        }else if ([JFStringHelper isNotEmpty:imagePath]){
             image = [UIImage imageWithContentsOfFile:imagePath];
        }
    }
    return image;
}

+(UIImage*)imageNameFromBuddle:(NSString*)name buddleName:(NSString*)buddlename{
   NSString* fileName = [name stringByDeletingPathExtension];
   NSString* extension = [name pathExtension];
   if ([JFStringHelper isEmpty:extension]) {
       extension = @"png";
   }
   return  [self imageNameFromBuddle:fileName type:extension buddleName:buddlename];
}
@end
